layui.use(['table', 'admin', 'ax','form','layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;
    /**
     * 档案柜信息表管理
     */
    var ArchiveCabinet = {
        tableId: "archiveCabinetTable"
    };

    /**
     * 初始化表格的列
     */
    ArchiveCabinet.initColumn = function () {
        return [[
            {field: 'id', width:80,hide: true, title: '主键'},
            {field: 'bh', width:100,sort: true, title: '编号'},
            {field: 'xh', width:140,sort: true, title: '序号'},
            {field: 'mc', width:120,sort: true, title: '名称'},
            {field: 'ssdasidName',  width:200, title: '所属档案室'},
            {field: 'ip',  width:120, title: 'ip地址'},
            {field: 'beiz',title: '备注'},
            {fixed:'right', width:120,align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ArchiveCabinet.search = function (wheres) {
        table.reload(ArchiveCabinet.tableId, {
            where: wheres, page: {curr: 1}
        });
    };


    //表单提交事件
    form.on('submit(btnSearch)', function (data) {
        ArchiveCabinet.search(data.field);
        return false;
    });




    /**
     * 弹出添加对话框
     */
    ArchiveCabinet.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/archiveCabinet/add';
    };

    /**
     * 导出excel按钮
     */
    ArchiveCabinet.exportExcel = function () {
        var checkRows = table.checkStatus(ArchiveCabinet.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveCabinet.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/archiveCabinet/edit?id=' + data.id;
    };


    function checkPwd(tips, call) {
        layer.prompt({
            formType: 1,
            title: tips + '，请输入【登陆密码】授权删除',
            area: ['400px', '400px'] //自定义文本域宽高
        }, function (value, index, elem) {
            layer.close(index);
            layer.prompt({
                formType: 1,
                title: tips + '，请再次输入【登陆密码】授权删除',
                area: ['400px', '400px'] //自定义文本域宽高
            }, function (two, index, elem) {
                if (value == two) {
                    call(two);
                } else {
                    Feng.error("两次密码不一致");
                }
                layer.close(index);
            });
        });
    }
    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveCabinet.onDeleteItem = function (data) {
        var operation = function () {
            var key = data.id;

            var ajax = new $ax(Feng.ctxPath + "/archiveCabinet/check", function (data) {
                Feng.success("删除成功!");
                table.reload(ArchiveCabinet.tableId);
            }, function (data) {
                checkPwd('当前档案柜内已有档案',function (pwd) {
                    var ajax = new $ax(Feng.ctxPath + "/archiveCabinet/delete", function (data) {
                        Feng.success("删除成功!");
                        table.reload(ArchiveCabinet.tableId);
                    }, function (data) {
                        Feng.error("删除失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("id", key);
                    ajax.set("pwd", pwd);
                    ajax.start();
                })
            });
            ajax.set("id", key);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ArchiveCabinet.tableId,
        url: Feng.ctxPath + '/archiveCabinet/list',
        page: true,
        height: "full-100",
        cellMinWidth: 100,
        cols: ArchiveCabinet.initColumn()
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ArchiveCabinet.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ArchiveCabinet.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ArchiveCabinet.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ArchiveCabinet.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ArchiveCabinet.onDeleteItem(data);
        }
    });
});
